// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoWorld;
import zombie.iso.objects.IsoFire;
import zombie.iso.objects.IsoFireManager;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.PacketValidator;
import zombie.network.ServerOptions;
import zombie.network.packets.hit.Square;

public class StartFire implements INetworkPacket {
    protected final Square square = new Square();
    protected int fireEnergy;
    protected boolean ignite;
    protected int life;
    protected boolean smoke;
    protected int spreadDelay;
    protected int numParticles;

    public void set(IsoGridSquare gridSquare, boolean _ignite, int _fireEnergy, int Life, boolean _smoke) {
        this.square.set(gridSquare);
        this.fireEnergy = _fireEnergy;
        this.ignite = _ignite;
        this.life = Life;
        this.smoke = _smoke;
        this.spreadDelay = 0;
        this.numParticles = 0;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.square.parse(b, connection);
        this.fireEnergy = b.getInt();
        this.ignite = b.get() == 1;
        this.life = b.getInt();
        this.smoke = b.get() == 1;
        if (GameClient.bClient) {
            this.spreadDelay = b.getInt();
            this.numParticles = b.getInt();
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.square.write(b);
        b.putInt(this.fireEnergy);
        b.putBoolean(this.ignite);
        b.putInt(this.life);
        b.putBoolean(this.smoke);
        if (GameServer.bServer) {
            b.putInt(this.spreadDelay);
            b.putInt(this.numParticles);
        }
    }

    public void process() {
        if (GameServer.bServer) {
            IsoFire isoFire0 = this.smoke
                ? new IsoFire(this.square.getSquare().getCell(), this.square.getSquare(), this.ignite, this.fireEnergy, this.life, true)
                : new IsoFire(this.square.getSquare().getCell(), this.square.getSquare(), this.ignite, this.fireEnergy, this.life);
            IsoFireManager.Add(isoFire0);
            this.spreadDelay = isoFire0.getSpreadDelay();
            this.numParticles = isoFire0.numFlameParticles;
            this.square.getSquare().getObjects().add(isoFire0);
        }

        if (GameClient.bClient) {
            IsoFire isoFire1 = this.smoke
                ? new IsoFire(IsoWorld.instance.CurrentCell, this.square.getSquare(), this.ignite, this.fireEnergy, this.life, true)
                : new IsoFire(IsoWorld.instance.CurrentCell, this.square.getSquare(), this.ignite, this.fireEnergy, this.life);
            isoFire1.SpreadDelay = this.spreadDelay;
            isoFire1.numFlameParticles = this.numParticles;
            IsoFireManager.Add(isoFire1);
            this.square.getSquare().getObjects().add(isoFire1);
        }
    }

    @Override
    public boolean isConsistent() {
        return this.square.getSquare() != null && this.life <= 500;
    }

    public boolean validate(UdpConnection connection) {
        if (GameServer.bServer && !this.smoke && ServerOptions.instance.NoFire.getValue()) {
            if (ServerOptions.instance.AntiCheatProtectionType16.getValue() && PacketValidator.checkUser(connection)) {
                PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type16", this.getDescription());
            }

            return false;
        } else if (!this.smoke && !IsoFire.CanAddFire(this.square.getSquare(), this.ignite, this.smoke)) {
            DebugLog.log("not adding fire that on " + this.square.getX() + "," + this.square.getY());
            if (ServerOptions.instance.AntiCheatProtectionType17.getValue() && PacketValidator.checkUser(connection)) {
                PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type17", this.getDescription());
            }

            return false;
        } else if (this.smoke && !IsoFire.CanAddSmoke(this.square.getSquare(), this.ignite)) {
            if (ServerOptions.instance.AntiCheatProtectionType18.getValue() && PacketValidator.checkUser(connection)) {
                PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type18", this.getDescription());
            }

            return false;
        } else {
            return GameClient.bClient || connection.RelevantTo(this.square.getX(), this.square.getY());
        }
    }

    @Override
    public String getDescription() {
        String string = "\n\t" + this.getClass().getSimpleName() + " [";
        string = string + "square=" + this.square.getDescription() + " | ";
        string = string + "fireEnergy=" + this.fireEnergy + " | ";
        string = string + "ignite=" + this.ignite + " | ";
        string = string + "life=" + this.life + " | ";
        string = string + "smoke=" + this.smoke + " | ";
        string = string + "spreadDelay=" + this.spreadDelay + " | ";
        return string + "numParticles=" + this.numParticles + "] ";
    }
}
